/*
 * This file contains all the javascript functions that use by
 * Syn user profile plugin in Synergy digital asset management
 * system.
 * Create by: Weranga Kaluarachchi
 * Date: 18/04/2010
 */



/*
 * Confirm delete message
 * @return true or false
 */
function matchNewPassword() {
	var retFlag = true;
	// return if element does not exist
	if(document.getElementById('txtSynNewUserPassword') == null) {
		retFlag = false;
    }
	// return if element does not exist
	else if(document.getElementById('txtSynUserConfirmPassword') == null) {
		retFlag = false;
    }
	else if (document.getElementById('txtSynNewUserPassword').value.length <6) {
		setError("lblErrPassword", "Your password must be at least 6 characters long!"); 
		retFlag = false;
	}
	else if (document.getElementById('txtSynNewUserPassword').value == document.getElementById('txtSynOldUserPassword').value) {
		alert("New password and old password can not be same");
		retFlag = false;
	}
	else if(document.getElementById('txtSynNewUserPassword').value != document.getElementById('txtSynUserConfirmPassword').value) {
		alert("The new password and the confirmation password don't match. Please type the same password in both boxes.");
		document.getElementById('txtSynNewUserPassword').value = "";
		document.getElementById('txtSynUserConfirmPassword').value = "";
		document.getElementById('txtSynNewUserPassword').focus();
		retFlag = false;
    }
	return retFlag;
}


/*
 * Check input values before submit the form
 * @return true or false
 */
function checkInputs() {
	var retFlag = "Ok";
	if(document.getElementById('txtSynProfileFirstName') != null && document.getElementById('txtSynProfileFirstName').value == "") {
		retFlag = "First name";
    }
	else if(document.getElementById('txtSynProfileDisplayName') != null && document.getElementById('txtSynProfileDisplayName').value == "") {
		retFlag = "Display name";
    }
	else if(document.getElementById('txtSynProfileEmail') != null && document.getElementById('txtSynProfileEmail').value == "") {
		retFlag = "Email";
    }
	return retFlag;
}


/*
 * Validate all the input values before submit the form
 * @return true or false
 */
function validateInputs() {
	var retFlag = true;
	var returnFlag = checkInputs();
	if(returnFlag != "Ok") {
		alert(returnFlag+" can not be empty!");
		retFlag = false;
	}
	else if (!validateEmail()) {
		retFlag = false;
	}
	else if(!ValidateForm()) {
		retFlag = false;
	}	
	return retFlag;
}

 
 /*
  * Toggle visibilty (show/hide) of divs based on user selection
  * @param	element ID of the element wants toggle
  * @param	object that trigger the function
  */
  function toggleChangePassword(objID, me) { 
 	 if(document.getElementById(objID) == null) {
 	        return;
 	    }
 	 if (document.getElementById(objID).style.display == "none") { 
 	  	document.getElementById(objID).style.display="inline"; 
 	  	me.innerHTML = "[Change Password <<]<br /><br />";
 	 } 
 	 else {
 		document.getElementById(objID).style.display="none"; 
 	    me.innerHTML = "[Change Password >>]";
 	 }
 }
  
  
 /*
  * Clear error text in error label
  * @param   error label
  */
  function clearError(errorlabel) {
  	if(document.getElementById(errorlabel) == null) {
          return;
      }
      // for IE browser   
      if(navigator.appName == "Microsoft Internet Explorer") {
          document.getElementById(errorlabel).innerText = "";   
      }
      // for firefox browser 
      else if(navigator.appName == "Netscape") {
          document.getElementById(errorlabel).textContent = "";
      }     
  }

  /*
   * Set error text in error label
   * @param   error label
   * @prama   error message
   */
  function setError(errorlabel, errorMsg) {
      if(document.getElementById(errorlabel) == null) {
          return;
      }
      if(navigator.appName == "Microsoft Internet Explorer") {
          // for IE browser
          document.getElementById(errorlabel).innerText = errorMsg;
      }
      else if(navigator.appName == "Netscape") {   
          // for firefox browser
          document.getElementById(errorlabel).textContent = errorMsg;
      }      
  }
  
  
  
  // --------------------------------------Phone Validate Section---------------------------------------
   /**
   * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
   */

  // Declaring required variables
  var digits = "0123456789";
  // non-digit characters which are allowed in phone numbers
  var phoneNumberDelimiters = "()- ";
  // characters which are allowed in international phone numbers
  // (a leading + is OK)
  var validWorldPhoneChars = phoneNumberDelimiters + "+";
  // Minimum no of digits in an international phone no.
  var minDigitsInIPhoneNumber = 10;

  function isInteger(s) { 
	  var i;
      for (i = 0; i < s.length; i++) {   
          // Check that current character is number.
          var c = s.charAt(i);
          if (((c < "0") || (c > "9"))) return false;
      }
      // All characters are numbers.
      return true;
  }
  function trim(s) { 
	  var i;
      var returnString = "";
      // Search through string's characters one by one.
      // If character is not a whitespace, append to returnString.
      for (i = 0; i < s.length; i++) {   
          // Check that current character isn't whitespace.
          var c = s.charAt(i);
          if (c != " ") returnString += c;
      }
      return returnString;
  }
  function stripCharsInBag(s, bag) {
	  var i;
      var returnString = "";
      // Search through string's characters one by one.
      // If character is not in bag, append to returnString.
      for (i = 0; i < s.length; i++) {   
          // Check that current character isn't whitespace.
          var c = s.charAt(i);
          if (bag.indexOf(c) == -1) returnString += c;
      }
      return returnString;
  }

  function checkInternationalPhone(strPhone){
	  var bracket=3;
	  strPhone=trim(strPhone);
	  if(strPhone.indexOf("+")>1) 
		  return false;
	  if(strPhone.indexOf("-")!=-1)
		  bracket=bracket+1;
	  if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)
		  return false;
	  var brchr=strPhone.indexOf("(");
	  if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")
		  return false;
	  if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)
		  return false;
	  var s=stripCharsInBag(strPhone,validWorldPhoneChars);
	  return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
  }


  /*
   * validate the phone number
   * @return true or false
   */
  function ValidateForm(){
  	var phoneValue=document.getElementById('txtSynProfilePhone').value;
  	if ((phoneValue==null)||(phoneValue=="")){
  		alert("Please enter your phone number!");
  		document.getElementById('txtSynProfilePhone').focus();
  		return false;
  	}
  	else if (checkInternationalPhone(phoneValue)==false){
  		alert("Please enter a valid phone number!");
  		document.getElementById('txtSynProfilePhone').value="";
  		document.getElementById('txtSynProfilePhone').focus();
  		return false;
  	}
  	return true;
 }

 //------------------------------------------End-------------------------------------------------
  
 
  /*
   * Validate the email address
   * @return true or false
   */
 function validateEmail() {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.getElementById('txtSynProfileEmail').value;
   if(reg.test(address) == false) {
      alert('Please enter a valid email address!');
      return false;
   }
   return true;
}

